clear all
set more off, perm
set maxvar 120000
set varabbrev off
* ---------------------------------------------- *
global dir 	"`1'"
global Data 	$dir/Data
global Tables 	$dir/Tables
global Figures 	$dir/Figures
global Work 	$dir/Work
global Temp 	$dir/Temp
global Pseudo	$dir/Work/Temp
* ---------------------------------------------- *
* figure out whether security is common stock (shrcd equal to 10 or 11)
import sas $Data/CRSP/stocknames.sas7bdat, clear case(lower)
keep permno permco comnam namedt nameenddt siccd shrcd exchcd 
sort permco permno nameenddt

* find end-of-the-month day for the last record of each stock and extend ending date through end of calendar year
by permco permno:  gen obs=_n
by permco permno:  gen tot=_N
replace nameenddt=mdy(12,31,year(nameenddt)) 			if month(nameenddt)==12 & obs==tot
replace nameenddt=mdy(month(nameenddt)+1,1,year(nameenddt))-1 	if month(nameenddt)<=11 & obs==tot

drop obs tot

tempfile header
save "`header'", replace
/* ********************************************************************* */
import sas $Data/CRSP/msf.sas7bdat, clear case(lower)
keep permno permco date prc shrout ret retx 

joinby permco permno using "`header'"
keep if date>=namedt & date<=nameenddt

keep if year(date)>1980 
replace  prc=abs(prc)
generate mcap=shrout*prc

keep if shrcd==10 | shrcd==11
keep if exchcd>=1 & exchcd<=3

collapse (sum) mcap, by(permco date)
gen datem=mofd(date)
format date %tm
tsset permco datem

forvalues x=1(1)60 {
	gen mcap`x'=F`x'.mcap
}

tempfile mcap
save "`mcap'", replace
* ************************************************
foreach var in qnic {
foreach PORTF in BOOK SIZE INV PROF MOME {
use permco datem BOOK SIZE INV PROF MOME mcap* ret_mo* `var'* using $Temp/tmp_portfolio_monthly, clear
merge 1:1 permco datem using "`mcap'"
keep if _merge==3
drop _merge

generate portf=1+int((`PORTF'-1)/2)    
keep if portf==1 | portf==5
tsset permco datem

forvalues x=1(1)60 {
replace  ret_mo`x'=. 	if `var'`x'   ==.
replace `var'`x'=. 	if  ret_mo`x' ==.
rename  `var'`x' squiggle`x'
}
forvalues x=1(1)60 {
local j=`x'-1
bys datem portf: egen tot`j'=total(mcap`j' * !missing(ret_mo`x'+squiggle`x'))
	replace ret_mo`x'   =(mcap`j'/tot`j')*ret_mo`x'
	replace squiggle`x' =(mcap`j'/tot`j')*squiggle`x'
drop tot`j'
}
collapse (sum) ret_* squig*, by(portf datem)
quietly: ds squig* ret*
foreach name of varlist `r(varlist)' {
	replace `name'=. if `name'==0
}
* *******************************
scalar alfa= 0.97749304

gen NretMo1=1+ret_mo1
gen qretMo1=1+squiggle1

gen qretQr1=1
gen NretQr1=1
forvalues x=1(1)3 {
replace  NretQr1=NretQr1*(1+ret_mo`x'  )
replace  qretQr1=qretQr1*(1+squiggle`x')
}
forvalues x=1(1)5 {
gen NretYr`x'=1
gen qretYr`x'=1
}
forvalues x=1(1)12 {
replace  NretYr1=NretYr1*(1+ret_mo`x')
replace  qretYr1=qretYr1*(1+squiggle`x')
}
forvalues x=13(1)24 {
replace  NretYr2=NretYr2*(1+ret_mo`x')
replace  qretYr2=qretYr2*(1+squiggle`x')
}
forvalues x=25(1)36 {
replace  NretYr3=NretYr3*(1+ret_mo`x')
replace  qretYr3=qretYr3*(1+squiggle`x')
}
forvalues x=37(1)48 {
replace  NretYr4=NretYr4*(1+ret_mo`x')
replace  qretYr4=qretYr4*(1+squiggle`x')
}
forvalues x=49(1)60 {
replace  NretYr5=NretYr5*(1+ret_mo`x')
replace  qretYr5=qretYr5*(1+squiggle`x')
}
gen cumNRetMo1=ln(NretMo1)
gen cumQRetMo1=ln(qretMo1) 

gen cumNRetQr1=ln(NretQr1)
gen cumQRetQr1=ln(qretQr1) 

gen cumNRetYr1=ln(NretYr1)
gen cumQRetYr1=ln(qretYr1) 

gen cumNRetYr3=ln(NretYr1)+alfa*ln(NretYr2)+alfa^2*ln(NretYr3)
gen cumQRetYr3=ln(qretYr1)+alfa*ln(qretYr2)+alfa^2*ln(qretYr3) 

gen cumNRetYr5=ln(NretYr1)+alfa*ln(NretYr2)+alfa^2*ln(NretYr3)+alfa^3*ln(NretYr4)+alfa^4*ln(NretYr5)
gen cumQRetYr5=ln(qretYr1)+alfa*ln(qretYr2)+alfa^2*ln(qretYr3)+alfa^3*ln(qretYr4)+alfa^4*ln(qretYr5)
* ************************************************************
local Z "cum*"
keep datem portf `Z'
* *******************
* Spreads:
preserve
keep if portf==1 | portf==5 
foreach name of varlist `Z' {
replace `name'=-`name' if portf==5 & index("`PORTF'","INV")+index("`PORTF'","BETA")+index("`PORTF'","SIZE")==1
replace `name'=-`name' if portf==1 & index("`PORTF'","INV")+index("`PORTF'","BETA")+index("`PORTF'","SIZE")==0
}
foreach v of varlist  `Z' {
bysort datem: egen c`v' = count(`v') 
replace `v'=. if c`v'<2
}
collapse (sum) `Z', by(datem)
foreach name of varlist  `Z' {
replace `name'=. if `name'==0
}
generate portf=6

tempfile tmp
save "`tmp'", replace
restore
append using "`tmp'"
* *******************
tsset portf datem
eststo clear
generate cumQ=cumQRetMo1	
eststo Mo1_`x': quietly: ivreg2 cumNRetMo1 cumQ if portf==6, bw(1 ) robust small

replace cumQ=cumQRetQr1	
eststo Qr1_`x': quietly: ivreg2 cumNRetQr1 cumQ if portf==6, bw(4 ) robust small

replace cumQ=cumQRetYr1	
eststo Yr1_`x': quietly: ivreg2 cumNRetYr1 cumQ if portf==6, bw(13)robust small

replace cumQ=cumQRetYr3	
eststo Yr3_`x': quietly: ivreg2 cumNRetYr3 cumQ if portf==6, bw(37)robust small

replace cumQ=cumQRetYr5	
eststo Yr5_`x': quietly: ivreg2 cumNRetYr5 cumQ if portf==6, bw(61)robust small

if "`PORTF'"=="BOOK" {
esttab Mo1_* Qr1_* Yr1_* Yr3_* Yr5_* using $Tables/AppendixB4_June_`var'_March.csv	,  nonotes compress b(4) se(4) star(c 0.10 b 0.05 a 0.01) nolabel nogap ar2 title(`PORTF') replace
}
if "`PORTF'"!="BOOK" {
esttab Mo1_* Qr1_* Yr1_* Yr3_* Yr5_* using $Tables/AppendixB4_June_`var'_March.csv	,  nonotes compress b(4) se(4) star(c 0.10 b 0.05 a 0.01) nolabel nogap ar2 title(`PORTF') append
}
}
}
